/*
 * CTDR_Animation.cpp
 *
 *  Created on: 6 déc. 2010
 *      Author: mbenoit
 */

#include "CTDR_Animation.h"

// @(#)codeTDv2Final/CTDR_Animation :$Name:  $:$Id: CTDR_Animation.cpp 16 2010-12-24 17:04:54Z ben.beland $
// Author: Mathieu Benoit   24/11/06

/*************************************************************************
 * . Class CTDR_Animation					                 *
 *   Written by : Mathieu Benoit                                         *
 *   m.benoit@umontreal.ca						 *
 *************************************************************************/

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string>
#include "TGeoManager.h"
#include "CTDR_Animation.h"
using namespace std;

//////////////////////////////////////////////////////////////////////////
// This class is used to produce a root file containing the tracks of  	//
// the charge elements simulated. It creates a geometry, defined in the //
// the constructor, and everything needed to draw image and Animation   //
// of the simulated event in the geometry . The root file can be read  	//
// using cint interpreter, and the geometry and the charge tracks can	//
// then be manipulated using functions provided with root's geometry  	//
// packages , see TGeoManager and TGeoTracks classes.	 		//
//////////////////////////////////////////////////////////////////////////

//ClassImp(CTDR_Animation)

//______________________________________________________________________________
CTDR_Animation::CTDR_Animation(double lx, double ly, double lz,CTDR_Interaction &electrons,CTDR_Interaction &holes ,int i , int j )
{
// This class builds a geometry and tracks object to put simulated data
// Lx,Ly,Lz are the dimensions of the box, inter, the interaction to be simulated, i and j ae ID for the
// root file name


                        Lx=lx;
                        Ly=ly;
                        Lz=lz;

                        cout << TString::Format("[CTDR_Animation] Initializing the tracking geometry") << endl;

                        filename=TString::Format("CTDR_Animation_%i_%i.root",i,j);
                        f2= new TFile(filename,"RECREATE");

                        TString eventname= TString::Format("Evenement_%i_%i",i,j);
                        Geo= new TGeoManager(eventname,"CTDR_Animation du detecteur");

                        Double_t ori[3]={Lx/2,Ly/2,Lz/2};
                        TGeoMedium *medium=0;
                        top = Geo->MakeBox("TOP",medium,Lx,Ly,Lz);
                        Pix= new TGeoBBox(eventname,Lx/2,Ly/2,Lz/2,ori);
                        pix =new  TGeoVolume(eventname,Pix);
                        top->AddNode(pix,1);
                        Geo->SetTopVolume(top);



//                        for(int i=0;i<4;i++){
//                                for(int j=0;j<4;j++){
//                                        sprintf(nom,"pix_%i_%i",i,j);
//                                        ori[0]=-0.5*(3.0/2)+ 0.5*i;
//                                        ori[1]=-0.5*(3.0/2)+ 0.5*j;;
//                                        ori[2]=(Lz/Lx)-1e-9;
//                                        Pix= new TGeoBBox(nom,0.25*125/225,0.25*125/225,1e-9,ori);
//                                        sprintf(nom,"Pix_%i_%i",i,j);
//                                        pix =new  TGeoVolume(nom,Pix);
//                                        pix->SetLineColor(kRed);
//                                        top->AddNode(pix,1);
//                                        };};


                     Geo->CloseGeometry();
                     top->SetLineColor(kMagenta);

                     Geo->SetTopVisible();
                     Ntracks=0;
                     Nelectrons=0;
                     Nholes=0;
                     for(int i=0;i<electrons.GetN();i++){
                                  Geo->AddTrack(i,100);
                                  Ntracks++;
                                  Nelectrons++;
                                  //cout << Geo->GetCurrentTrack() << endl;
                                 // Geo->GetCurrentTrack()->SetLineColor(1);
                                 // Geo->GetCurrentTrack()->SetMarkerSize(4);
                                  };
                     for(int i=electrons.GetN();i<electrons.GetN()+holes.GetN();i++){
                                  Geo->AddTrack(i,100);
                                  Ntracks++;
                                  Nholes++;
                                  //cout << Geo->GetCurrentTrack() << endl;
                                  //Geo->GetCurrentTrack()->SetLineWidth(1);
                                 // Geo->GetCurrentTrack()->SetMarkerSize(4);
                                  };

};

//______________________________________________________________________________
void CTDR_Animation::AjoutePas(CTDR_Interaction &electrons,CTDR_Interaction &holes)
{
// This method adds a step to the Animation by reading the data from the simulated interaction
	int n=0;
     for(int i=0;i<electrons.GetN();i++){
             Geo->GetTrack(n)->AddPoint
             (electrons.GetCharge(i).GetX(),electrons.GetCharge(i).GetY(),(electrons.GetCharge(i).GetZ()),electrons.Gett());
             n++;
     };

     for(int i=0;i<holes.GetN();i++){
    	 Geo->GetTrack(n)->AddPoint
    	              (holes.GetCharge(i).GetX(),holes.GetCharge(i).GetY(),(holes.GetCharge(i).GetZ()),holes.Gett());
    	 	 n++;


     };


};

//______________________________________________________________________________
void CTDR_Animation::AjoutePasHoles(CTDR_Interaction &electrons,CTDR_Interaction &holes)
{
// This method adds a step to the Animation by reading the data from the simulated interaction
	int n=electrons.GetN();


     for(int i=0;i<holes.GetN();i++){
    	 Geo->GetTrack(n)->AddPoint
    	              (holes.GetCharge(i).GetX(),holes.GetCharge(i).GetY(),(holes.GetCharge(i).GetZ()),holes.Gett());
    	 	 n++;


     };


};

//______________________________________________________________________________
void CTDR_Animation::AjoutePasElectrons(CTDR_Interaction &electrons,CTDR_Interaction &holes)
{
// This method adds a step to the Animation by reading the data from the simulated interaction
	int n=0;
     for(int i=0;i<electrons.GetN();i++){
             Geo->GetTrack(n)->AddPoint
             (electrons.GetCharge(i).GetX(),electrons.GetCharge(i).GetY(),(electrons.GetCharge(i).GetZ()),electrons.Gett());
             n++;
     };


     };






//______________________________________________________________________________
void CTDR_Animation::Write()
{
// This function write the final objects to a root file


	cout << TString::Format("[CTDR_Animation] Writing Geometry to file %s",filename.Data()) << endl;
	for(int i=0;i<Ntracks;i++){
		Geo->GetTrack(i)->SetLineWidth(0.1);

		if(i<Nelectrons){
			Geo->GetTrack(i)->SetLineColor(kBlue);
			Geo->GetTrack(i)->SetMarkerColor(kBlue);

		}
		else{

			Geo->GetTrack(i)->SetLineColor(kRed);
			Geo->GetTrack(i)->SetMarkerColor(kRed);
		};

	};

     //Geo->AnimateTracks(0,Tmax,Npas);
     Geo->Write();
     f2->Close();
};



